package plantshop.tests;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import plantshop.model.Store;
import plantshop.model.plants.*;
import plantshop.view.View;

public class InvasivePlantTest {

	InvasivePlant test;
	Plant other1,other2,other3,other4,other5;
	Store store;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		test = new BurningBush();
		other1 = new Aster();
		other2 = new CommonMilkweed();
		other3 = new WhiteOak();
		other4 = new JapaneseBarberry();
		other5 = new NorwayMaple();
		store = new Store(300,View.xscale,View.yscale);
	}

	@After
	public void tearDown() throws Exception {
		test = null;
		other1 = null;
		other2 = null;
		other3 = null;
		other4 = null;
		other5 = null;
		store = null;
	}

	@Test
	public void testTakeOver() {
		double chance = 0.35;
		double tolerance = 0.1;
		int times = 0;
		int total = 1000;
		
		for(int i = 0;i<total;i++){
			if(test.takeOver()){
				times++;
				test.resetSpawns();
			}
		}
		
		double actual = (double)times/total;
		assertTrue(actual >= chance*(1-tolerance) && actual <= chance*(1+tolerance));
	}
	
	@Test
	public void testGetSurroundingNatives(){
		List<Plant> surroundings = new ArrayList<Plant>();
		surroundings.add(other2);
		surroundings.add(other3);
		
		store.addPlant(true, 0, 3, test);
		store.addPlant(true, 0, 0, other1);
		store.addPlant(true, 0, 1, other2);
		store.addPlant(true, 0, 2, other3);
		store.addPlant(true, 0, 4, other4);
		store.addPlant(true, 0, 5, other5);
		
		assertTrue("Expected: " + surroundings.toString() + "\nWas: " + test.getSurroundingNatives(store.getFront().get(0)).toString(),surroundings.containsAll(test.getSurroundingNatives(store.getFront().get(0))));
		
		store = null;
		store = new Store(300,View.xscale,View.yscale);
		store.addPlant(true, 0, 0, test);
		store.addPlant(true, 0, 1, other1);
		store.addPlant(true, 0, 2, other2);
		store.addPlant(true, 0, 3, other3);
		store.addPlant(true, 0, 4, other4);
		store.addPlant(true, 0, 5, other5);
		
		surroundings = new ArrayList<Plant>();
		surroundings.add(other1);
		surroundings.add(other2);
		
		assertTrue("Expected: " + surroundings.toString() + "\nWas: " + test.getSurroundingNatives(store.getFront().get(0)).toString(),surroundings.containsAll(test.getSurroundingNatives(store.getFront().get(0))));
		
		store = null;
		store = new Store(300,View.xscale,View.yscale);
		store.addPlant(true, 0, 7, test);
		store.addPlant(true, 0, 5, other1);
		store.addPlant(true, 0, 2, other2);
		store.addPlant(true, 0, 3, other3);
		store.addPlant(true, 0, 4, other4);
		store.addPlant(true, 0, 6, other5);
		
		surroundings = new ArrayList<Plant>();
		surroundings.add(other1);
		
		assertTrue("Expected: " + surroundings.toString() + "\nWas: " + test.getSurroundingNatives(store.getFront().get(0)).toString(),surroundings.containsAll(test.getSurroundingNatives(store.getFront().get(0))));
		
		store = null;
		store = new Store(300,View.xscale,View.yscale);
		store.addPlant(true, 0, 4, test);
		store.addPlant(true, 0, 2, other1);
		store.addPlant(true, 0, 3, other2);
		store.addPlant(true, 0, 5, other3);
		store.addPlant(true, 0, 6, other4);
		store.addPlant(true, 0, 7, other5);
		
		surroundings = new ArrayList<Plant>();
		surroundings.add(other1);
		surroundings.add(other3);
		
		assertTrue("Expected: " + surroundings.toString() + "\nWas: " + test.getSurroundingNatives(store.getFront().get(0)).toString(),surroundings.containsAll(test.getSurroundingNatives(store.getFront().get(0))));
	}
}
